<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // function getSum(arr) {
      //         let length = arr.length
      //         if (length === 0) return 0
      //         if (length === 1) return arr[0]
      //         //设置一个存最大值的数组
      //         let dp = [arr[0], Math.max(arr[0], arr[1])]

      //         //后面的比较就要运用循环来进行比较
      //         for (let i = 2; i < arr.length; i++) {
      //           //一直在数组里面追加最大值追加
      //             dp[i] = Math.max(arr[i] + dp[i - 2], dp[i - 1])
      //             console.log(dp[i]);
      //             console.log(dp);
      //         }
      //         //返回最优解数组的最后一项
      //         return dp[length - 1]
      //     }

      //     let res = getSum([2, 7, 4,5,9,4,1])
      //     console.log(res);

      function fn(arr = []) {
        console.log(arr);
        
        let length = arr.length
        if (length == 0) return 0
        if (length == 1) return arr[0]
        let lp = [arr[0], Math.max(arr[0], arr[1])]
        for (let i = 2; i < arr.length; i++) {
          lp[i] =  Math.max(arr[i]+ lp[i - 2],lp[i - 1])
        }
        return lp[length - 1]
      }
      let res = fn([2, 4, 6, 5, 4, 7])
      console.log(res)
    </script>
  </body>
</html>
